package edu.task.service.impl;

import edu.task.mapper.Mapper;
import edu.task.model.SysExplain;
import edu.task.model.SysRole;
import edu.task.model.SysRoleMenu;
import edu.task.model.SysUserExplain;
import edu.task.model.vo.SysExplainVO;
import edu.task.repository.SysExplainRepository;
import edu.task.repository.SysUserExplainRepository;
import edu.task.service.SysExplainService;
import edu.task.utils.BeanUtils;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class SysExplainServiceImpl implements SysExplainService {
    @Autowired
    SysExplainRepository sysExplainRepository;

    @Autowired
    Mapper mapper;

    @Autowired
    SysUserExplainRepository sysUserExplainRepository;

    @Override
    public List<SysExplain> selectExplainList() {
        return sysExplainRepository.findAll();
    }

    @Override
    public boolean insertExplain(SysExplainVO explain) {
        SysExplain sysExplain = new SysExplain();
        BeanUtils.copyProperties(explain, sysExplain);
        mapper.insertExplain(sysExplain);
        explain.setExplainId(sysExplain.getExplainId());
        insertUserExplain(explain);
        return null != sysExplain.getExplainId();
    }

    @Override
    @Transactional
    public int deleteExplainById(Long explainId) {
        sysExplainRepository.deleteUserExplainByExplainId(explainId);
        return sysExplainRepository.deleteExplainById(explainId);
    }

    @Override
    @Transactional
    public boolean updateExplain(SysExplainVO explain) {
        Long explainId = explain.getExplainId();
        Optional<SysExplain> op = sysExplainRepository.findById(explainId);
        if (!op.isPresent()) {
            return false;
        }
        // 删除用户与个人说明关联
        sysExplainRepository.deleteUserExplainByExplainId(explainId);
        SysExplain sysExplain = op.get();
        BeanUtils.copyPropertiesIgnoreNull(explain, sysExplain);
        //更新个人说明信息
        sysExplainRepository.save(sysExplain);
        //新增用户和个人说明关联
        insertUserExplain(explain);
        return null != sysExplain.getExplainId();
    }

    @Override
    public List<SysExplain> selectExplainListByUserId(Long userId) {
        return sysExplainRepository.selectExplainListByUserId(userId);
    }

    @Override
    @Transactional
    public int deleteExplainByIds(Long[] explainIds) {
        for (int i=0;i<explainIds.length;i++){
            sysExplainRepository.deleteUserExplainByExplainId(explainIds[i]);
            sysExplainRepository.deleteExplainById(explainIds[i]);
        }
        return 1;
    }

    @Transactional
    public int insertUserExplain(SysExplainVO explain) {
        Long[] users = explain.getUserIds();
        if (StringUtils.isNull(users) || users.length == 0) {
            return 0;
        }
        Long[] explainIds = new Long[users.length];
        Long explainId = explain.getExplainId();
        for (Long userId : users) {
            SysUserExplain sysUserExplain = new SysUserExplain();
            sysUserExplain.setExplainId(explainId);
            sysUserExplain.setUserId(userId);
            sysUserExplainRepository.save(sysUserExplain);
        }
        Arrays.fill(explainIds, explain.getExplainId());
        return 1;
    }
}
